package com.example.lj.uav.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lj.uav.R;
import com.example.lj.uav.utils.OnUIUtils;

import dji.common.error.DJIError;
import dji.common.flightcontroller.CompassCalibrationState;
import dji.common.util.CommonCallbacks;
import dji.sdk.flightcontroller.Compass;

public class CompassDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private Compass compass;
    private TextView tv_title, tv_content, tv_cancel, tv_submit;
    private View v_pic, v_split;
    private LinearLayout ll_fail;
    private View tv_prc_cancel;
    private View tv_suc_submit;

    public CompassDialog(@NonNull Context context, Compass compass) {
        super(context);
        this.context = context;
        this.compass = compass;
    }

    public CompassDialog(@NonNull Context context, Compass compass, int themeResId) {
        super(context, themeResId);
        this.context = context;
        this.compass = compass;
    }

    protected CompassDialog(@NonNull Context context, Compass compass, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
        this.compass = compass;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_compass);
        setCanceledOnTouchOutside(false);

    }

    @Override
    protected void onStart() {
        super.onStart();
        initView();
        compass.startCalibration(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onResult(DJIError djiError) {
                if (djiError != null) {
                    OnUIUtils.setToast("启动失败");
                    showProcessView(3);
                }
            }
        });
    }

    private void initView() {
        tv_title = findViewById(R.id.compass_tv_title);
        tv_content = findViewById(R.id.compass_tv_content);
        v_pic = findViewById(R.id.compass_v_pic);
        tv_prc_cancel = findViewById(R.id.compass_tv_prc_cancel);
        tv_prc_cancel.setOnClickListener(this);
        tv_suc_submit = findViewById(R.id.compass_tv_suc_submit);
        tv_suc_submit.setOnClickListener(this);
        ll_fail = findViewById(R.id.compass_ll_fail);
        findViewById(R.id.compass_tv_fail_cancel).setOnClickListener(this);
        findViewById(R.id.compass_tv_fail_submit).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.compass_tv_prc_cancel:
                compass.stopCalibration(new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onResult(DJIError djiError) {

                    }
                });
            case R.id.compass_tv_fail_cancel:
            case R.id.compass_tv_suc_submit:
                dismiss();
                break;
            case R.id.compass_tv_fail_submit:
                startCal();
                break;
        }
    }

    private void startCal() {
        compass.setCalibrationStateCallback(new CompassCalibrationState.Callback() {
            @Override
            public void onUpdate(@NonNull CompassCalibrationState compassCalibrationState) {
                showProcessView(compassCalibrationState.value());
            }
        });
    }

    private void showProcessView(int step) {
        switch (step) {
            case 0:
                OnUIUtils.setTextView(tv_title, context.getResources().getString(R.string.compass_calibration_process));
                OnUIUtils.setTextView(tv_content, context.getResources().getString(R.string.compass_calibration_horizontal));
                OnUIUtils.setBackground(v_pic, R.drawable.fpv_compass_horizontal_m210);
                if (ll_fail.getVisibility() != View.GONE)
                    OnUIUtils.setViewVisibility(ll_fail, View.GONE);
                if (tv_suc_submit.getVisibility() != View.GONE)
                    OnUIUtils.setViewVisibility(tv_suc_submit, View.GONE);
                OnUIUtils.setViewVisibility(tv_prc_cancel, View.VISIBLE);
                break;
            case 1:
                OnUIUtils.setTextView(tv_title, context.getResources().getString(R.string.compass_calibration_process));
                OnUIUtils.setTextView(tv_content, context.getResources().getString(R.string.compass_calibration_vertical));
                OnUIUtils.setBackground(v_pic, R.drawable.fpv_compass_vertical_m210);
                if (ll_fail.getVisibility() != View.GONE)
                    OnUIUtils.setViewVisibility(ll_fail, View.GONE);
                if (tv_suc_submit.getVisibility() != View.GONE)
                    OnUIUtils.setViewVisibility(tv_suc_submit, View.GONE);
                OnUIUtils.setViewVisibility(tv_prc_cancel, View.VISIBLE);
                break;
            case 2:
                OnUIUtils.setTextView(tv_title, context.getResources().getString(R.string.compass_calibration_suc));
                OnUIUtils.setViewVisibility(tv_content, View.GONE);
                OnUIUtils.setBackground(v_pic, R.drawable.sc_success);
                if (ll_fail.getVisibility() != View.GONE)
                    OnUIUtils.setViewVisibility(ll_fail, View.GONE);
                if (tv_prc_cancel.getVisibility() != View.GONE)
                    OnUIUtils.setViewVisibility(tv_prc_cancel, View.GONE);
                OnUIUtils.setViewVisibility(tv_suc_submit, View.VISIBLE);
                break;
            case 3:
                OnUIUtils.setTextView(tv_title, context.getResources().getString(R.string.compass_calibration_fai));
                OnUIUtils.setViewVisibility(tv_content, View.GONE);
                OnUIUtils.setBackground(v_pic, R.drawable.sc_error);
                if (tv_suc_submit.getVisibility() != View.GONE)
                    OnUIUtils.setViewVisibility(tv_suc_submit, View.GONE);
                if (tv_prc_cancel.getVisibility() != View.GONE)
                    OnUIUtils.setViewVisibility(tv_prc_cancel, View.GONE);
                OnUIUtils.setViewVisibility(ll_fail, View.VISIBLE);
                break;
        }
    }
}
